package com.china08.yxyapi.api.manage.sch.platform.xiaoben.kecheng;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLesson;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class KeChengLessonIsVisibleUpdController extends YxyApiBaseController {

    @Autowired
    YxyLessonsSchoolLessonRepository lessonRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/sch/xiaoben/kecheng/lesson/isvisible/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String id = reqModel.id;
        String downReason = reqModel.downReason;
        Integer isvisible = reqModel.isvisible;
        String downType = reqModel.downType;

        YxyLessonsSchoolLesson lesson = lessonRepository.findById(id);
        if (null != lesson) {
            lesson.setIsVisible(isvisible);
            lesson.setDownReason(downReason);
            lesson.setDownType(downType.toString());

            Date date = new Date();
            // 上架课程
            if (isvisible == 1) {
                lesson.setUpDate(date);
            }
            // 下架课程
            if (isvisible == 0) {
                lesson.setDownDate(date);
            }
            lessonRepository.save(lesson);
        }

    }

    public static class ReqModel extends YxyApiPageReqModel {
        public String id;
        public Integer isvisible;
        public String downType;
        public String downReason;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(id), "未获取课时");
        }
    }
}
